package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.common.Result;
import com.woniuxy.common.StatusCode;
import com.woniuxy.domain.Dealtime;
import com.woniuxy.domain.Recordbug;
import com.woniuxy.dto.DtoMyPage;
import com.woniuxy.dto.DtoSubmitBugInfo;
import com.woniuxy.dto.DtoSubmitBugInfoVO;
import com.woniuxy.service.DealtimeService;
import com.woniuxy.service.RecordbugService;
import com.woniuxy.util.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BugHandlingController
 * @Description 当前登录用户的缺陷任务查询与提交
 * @Author Zcd
 * @Version 1.0
 **/
@RestController
@RequestMapping("/submitBug")
@Api(tags = "缺陷任务进程提交")
public class SubmitBugController {

    @Resource
    private RecordbugService recordbugService;

    @Resource
    private DealtimeService dealtimeService;

    @Resource
    private MinioUtil minioUtil;

    @GetMapping("allBug")
    @ApiOperation("获取登录用户所有缺陷任务")
    /**
     * @Author： Zcd
     * @Description: 获取登录用户所有缺陷任务
     * @Param: [page]
     * @Return: com.woniuxy.common.Result
     */
    public Result getAllBug(int current, int size, int userId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        DtoMyPage<DtoSubmitBugInfo> dtoMyPage = recordbugService.searchForAllBug(current, size, userId);
        return new Result(true, StatusCode.OK, "操作成功", dtoMyPage);
    }

    @GetMapping("allProject")
    @ApiOperation("获取登录用户所处理的所有bug的所属项目")
    /**
     * @Author： Zcd
     * @Description: 获取该用户所处理的所有bug的所属项目
     * @Param: [userId]
     * @Return: com.woniuxy.common.Result
     */
    public Result getAllProject(int userId) {
        ArrayList<Integer> allProject = recordbugService.searchForAllProject(userId);
        return new Result(true, StatusCode.OK, "操作成功", allProject);
    }

    @GetMapping("searchBugInfo")
    @ApiOperation("模糊查询该登录用户所查询相关缺陷内容")
    /**
    * @Author： Zcd
    * @Description: 模糊查询该登录用户所查询相关缺陷内容
    * @Param: [project, state, info]
    * @Return: com.woniuxy.common.Result
    */
    public Result getBugInfo(String projectId, String state, String info, int userId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        ArrayList<DtoSubmitBugInfo> arrayList = recordbugService.searchByInfo(projectId, state, info, userId);
        return new Result(true, StatusCode.OK, "操作成功", arrayList);
    }

    @PostMapping("uploadFile")
    @ApiOperation("提交修改好的缺陷文件")
    /**
     * @Author： Zcd
     * @Description: 提交修改好的缺陷文件
     * @Param: [file]
     * @Return: com.woniuxy.common.Result
     */
    public Result uploadBugFile(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String filename = file.getOriginalFilename();
        return minioUtil.uploadToMinio(inputStream, filename);
    }

    @PostMapping("saveRecord")
    @ApiOperation("上传缺陷文件成功之后存入DB并记录,并修改")
    /**
    * @Author： Zcd
    * @Description: 上传缺陷文件成功之后存入DB并记录,并修改
    * @Param: [dtoSubmitBugInfoVO]
    * @Return: com.woniuxy.common.Result
    */
    public Result saveRecord(@RequestBody DtoSubmitBugInfoVO dtoSubmitBugInfoVO) {
        Dealtime record = dealtimeService.createRecord(dtoSubmitBugInfoVO);
        dealtimeService.save(record);
        boolean isSuccess = recordbugService.updateState(dtoSubmitBugInfoVO);
        if (isSuccess) {
            return new Result(true, StatusCode.OK, "操作成功");
        } else {
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }
}
